package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.ProductKey;
import com.ruoyi.system.domain.vo.ClientProductKeyListRes;
import com.ruoyi.system.domain.vo.ClientProductKeyListVo;
import com.ruoyi.system.mapper.ProductKeyMapper;
import com.ruoyi.system.service.IProductKeySevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ProductKeyServiceImpl extends ServiceImpl<ProductKeyMapper, ProductKey> implements IProductKeySevice {

    @Autowired
    private ProductKeyMapper productKeyMapper;

    @Override
    public List<ClientProductKeyListRes> queryClientProductKey(Page<ClientProductKeyListVo> page, ClientProductKeyListVo clientProductKeyListVo) {
        List<ClientProductKeyListRes> res = productKeyMapper.queryClientProductKey(clientProductKeyListVo);
        return res;
    }

    @Override
    public ClientProductKeyListRes queryClientProductKey(ClientProductKeyListVo clientProductKeyListVo) {
        List<ClientProductKeyListRes> res = productKeyMapper.queryClientProductKey(clientProductKeyListVo);
        return CollectionUtils.isEmpty(res) ? null : res.get(0);
    }

    @Override
    public List<ProductKey> getNoSellProductKeysAndLimitCount(String productId) {
         return this.list(
                    Wrappers.lambdaQuery(ProductKey.class)
                            .eq(ProductKey::getProductId, productId)
                            .eq(ProductKey::getIsSell, "N")
                            .isNull(ProductKey::getPaymentRecordId));
    }

    @Override
    public List<ProductKey> queryNotSelledProductKey(Long id) {
        return this.list(
                Wrappers.lambdaQuery(ProductKey.class)
                        .eq(ProductKey::getProductId, id)
                        .eq(ProductKey::getIsSell, "N")
        );
    }
}
